package cn.zyq.ykb.service.sys.impl;


import cn.zyq.ykb.beans.sys.SysAdmin;
import cn.zyq.ykb.beans.sys.SysDictData;
import cn.zyq.ykb.mapper.sys.SysDictDataMapper;
import cn.zyq.ykb.service.sys.SysDictDataService;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.common.PageParam;
import cn.zyq.ykb.system.config.redis.RedisService;
import cn.zyq.ykb.system.ry.CacheConstants;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典数据表
 * 2023-05-12 20:14:08
 */

@Slf4j
@Service
public class SysDictDataServiceImpl extends BaseService implements SysDictDataService {
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private RedisService redisService;

    @Override
    public PageInfo<SysDictData> list(PageParam pageParam, SysAdmin admin) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        List<SysDictData> dbList = this.sysDictDataMapper.list(pageParam.getKv());
        return new PageInfo<>(dbList);
    }

    @Override
    public void add(SysDictData param, SysAdmin admin) {
        this.setCreateBaseData(param, admin);
        this.sysDictDataMapper.insertSelective(param);
    }

    @Override
    public void update(SysDictData param, SysAdmin admin) {
        Integer id = param.getSysDictDataID();
        this.setUpdateBaseData(param, admin);
        this.sysDictDataMapper.updateByPrimaryKeySelective(param);
    }

    @Override
    public void del(String serviceID, SysAdmin admin) {
        SysDictData dbInfo = this.detail(serviceID);
        Integer id = dbInfo.getSysDictDataID();
        this.deleteLogic(id, admin, this.sysDictDataMapper);
    }

    @Override
    public SysDictData detail(String serviceID, SysAdmin admin) {
        return this.detail(serviceID);
    }

    @Override
    public SysDictData detail(String serviceID) {
        SysDictData dbInfo = this.getByServiceID(serviceID, SysDictData.class, this.sysDictDataMapper);
        this.checkLogicExist(dbInfo);
        return dbInfo;
    }

    /**
     * 根据条件分页查询字典数据
     *
     * @param param 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(PageParam param) {
        return sysDictDataMapper.list(param.getKv());
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType  字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue) {
        return sysDictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return sysDictDataMapper.selectByPrimaryKey(dictCode);
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes) {
        for (Long dictCode : dictCodes) {
            SysDictData data = selectDictDataById(dictCode);
            sysDictDataMapper.deleteByPrimaryKey(dictCode);
            List<SysDictData> dictDatas = sysDictDataMapper.selectDictDataByType(data.getDictType());
            redisService.setCacheObject(getCacheKey(data.getDictType()), dictDatas);
        }
    }

    /**
     * 新增保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData data) {
        this.setCreateBaseData(data, SecurityUtils.getLoginUser().getSysAdmin());
        int row = this.sysDictDataMapper.insertSelective(data);
        if (row > 0) {
            List<SysDictData> dictDatas = sysDictDataMapper.selectDictDataByType(data.getDictType());
            redisService.setCacheObject(getCacheKey(data.getDictType()), dictDatas);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData data) {
        this.setUpdateBaseData(data, SecurityUtils.getLoginUser().getSysAdmin());
        int row =  this.sysDictDataMapper.updateByPrimaryKeySelective(data);
        if (row > 0) {
            List<SysDictData> dictDatas = sysDictDataMapper.selectDictDataByType(data.getDictType());
            redisService.setCacheObject(getCacheKey(data.getDictType()), dictDatas);
        }
        return row;
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey) {
        return CacheConstants.SYS_DICT_KEY + configKey;
    }
}
